package com.example.c4;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;

/**
 * @author Jonathan
 * 2021/5/28 21:17
 */

@Slf4j
public class WriteServer {

    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);//  设置为非阻塞模式
        System.out.printf("register key:%s\n", serverSocketChannel);
        // 注册
        SelectionKey sscKey = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        serverSocketChannel.bind(new InetSocketAddress(8080));

        while (true) {
            // select 在事件未处理时，他不会再阻塞
            // 事件发生后 要么处理 那么取消  不能置之不理
            selector.select();// 没有事件阻塞在这个位置

            // 处理事件  内部包含了所有的事件
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                // 移除
                iterator.remove();
                System.out.printf("key:%s\n", key);
//                key.cancel();
                // 建立连接
                if (key.isAcceptable()) {
                    // 只有serverSocketChannel 才能有这个事件
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    SelectionKey sckey = socketChannel.register(selector, 0, null);
                    sckey.interestOps(SelectionKey.OP_READ);
                    System.out.printf("建立连接socketChannel:%s\n", socketChannel);
                    socketChannel.configureBlocking(false);
                    // 准备大量数据
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < 3000000; i++) {
                        builder.append("a");
                    }
                    ByteBuffer buffer = Charset.defaultCharset().encode(builder.toString());
                    int write = socketChannel.write(buffer);
                    System.out.printf("write:%d\n", write);
                    // 返回值代表实际写入的数量
                    if (buffer.hasRemaining()) {
                        sckey.interestOps(sckey.interestOps() + SelectionKey.OP_WRITE);
                        sckey.attach(buffer);
                    }
                } else if (key.isReadable()) {
                    ByteBuffer buffer = (ByteBuffer) key.attachment();
                    SocketChannel sc = (SocketChannel) key.channel();
                    int write = sc.write(buffer);
                    System.out.printf("write:%d", write);
                    if (!buffer.hasRemaining()) {
                        key.attach(null);
                        key.interestOps(key.interestOps() - SelectionKey.OP_WRITE);
                    }
                }
            }

        }
    }

}
